﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.Test            Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: WorkerTest.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using System.Threading;
using Hyldahl.Services.TaskProcessing.Test.Dummy;
using Hyldahl.Services.TaskProcessing.Workers;
using NUnit.Framework;

namespace Hyldahl.Services.TaskProcessing.Test
{
    
    
    /// <summary>
    ///This is a test class for WorkerTest and is intended
    ///to contain all WorkerTest Unit Tests
    ///</summary>
    [TestFixture()]
    public class WorkerTest
    {
        /// <summary>
        ///A test for State
        ///</summary>
        [Test()]
        public void StateTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");

            WorkerState expected = WorkerState.Stopped;
            
            WorkerState actual = target.State;

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for StopFlag
        ///</summary>
        [Test()]
        public void StopFlagTest()
        {
            DummyWorker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");

            Assert.AreEqual(false, target.StopFlag);

            target.Start();
            Thread.Sleep(100);

            target.Stop();

            Assert.AreEqual(true, target.StopFlag);

            target.WaitForCompletion(new TimeSpan(1));
        }

        /// <summary>
        ///A test for Stop
        ///</summary>
        [Test()]
        public void StopNotStartedTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");
            target.Stop();
        }

        /// <summary>
        ///A test for Stop
        ///</summary>
        [Test()]
        public void StopTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");
            target.Start();
            target.Stop();
            target.WaitForCompletion();

            Assert.AreEqual(WorkerState.Stopped, target.State);
        }

        /// <summary>
        ///A test for Start
        ///</summary>
        [Test()]
        public void StartTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");
            
            target.Start();
            Assert.AreEqual(WorkerState.Started, target.State);

            target.Stop();
            target.WaitForCompletion(new TimeSpan(1));
        }

        /// <summary>
        ///A test for Kill
        ///</summary>
        [Test()]
        public void KillTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");
            target.Start();

            Thread.Sleep(100);

            target.Kill();

            Assert.AreEqual(WorkerState.Stopped, target.State);
        }

        /// <summary>
        ///A test for Kill
        ///</summary>
        [Test()]
        public void KillNotStartedTest()
        {
            Worker target = new DummyWorker(new TaskProcessingController(new DummyContext()), "Dummy");

            target.Kill();
        }
    }
}
